package com.nanbei.rabbit.producer.confirm;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassDescription: 延迟队列 = ttl（过期时间） + 死信队列  ，消费者监听死信队列
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/9/2 10:59
 */
@Configuration
public class DelayDirectExchangeConfig {

    // 创建死信交换机
    @Bean
    public Exchange delayDirectExchange(){
        return ExchangeBuilder.directExchange("delay_direct_exchange").durable(true).build();
    }

    // 创建死信队列
    @Bean
    public Queue delayDirectQueue(){
        return QueueBuilder.durable("delay_direct_queue")
                .maxLength(10L)    // 设置队列的最大存放长度
                .build();
    }

    //绑定死信队列和死信交换机
    @Bean
    public Binding delayDirectExchange2Queue(){
        return BindingBuilder.bind(delayDirectQueue()).to(delayDirectExchange()).with("delay").noargs();
    }



    // 创建正常交换机
    @Bean
    public Exchange lifeDelayDirectExchange(){
        return ExchangeBuilder.directExchange("life_delay_direct_exchange").durable(true).build();
    }

    // 创建正常队列
    @Bean
    public Queue lifeDelayDirectQueue(){
        return QueueBuilder.durable("life_delay_direct_queue")
                .ttl(20000)    // 设置队列的过期时间为20s
                .deadLetterExchange("delay_direct_exchange")  // 设置死信交换机
                .deadLetterRoutingKey("delay") // 设置死信交换机routingKey
                .build();
    }

    //绑定正常队列和正常交换机
    @Bean
    public Binding lifeDelayDirectExchange2Queue(){
        return BindingBuilder.bind(lifeDelayDirectQueue()).to(lifeDelayDirectExchange()).with("life").noargs();
    }



}
